package com.caine.object;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

@SpringBootApplication
public class ObjectBootApplication {
    private static final String FACTORIES_RESOURCE_LOCATION = "abc.txt";

    public static void main(String[] args) {
//        SpringApplication application = new SpringApplication(ObjectBootApplication.class);
//        application.setWebApplicationType(WebApplicationType.NONE);
//        application.run(args);
//        try {
//            loadProperty(ObjectBootApplication.class.getClassLoader());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        System.out.println(Integer.MAX_VALUE);
    }


    static void loadProperty(ClassLoader classLoader) throws Exception {
        MultiValueMap<String, String> result = null;

        Enumeration<URL> urls = (classLoader != null ?
                classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
                ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
        result = new LinkedMultiValueMap<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            System.out.println("url.toString() = " + url.toString());
            UrlResource resource = new UrlResource(url);
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            for (Map.Entry<?, ?> entry : properties.entrySet()) {
                String factoryTypeName = ((String) entry.getKey()).trim();
                System.out.println("key:" + factoryTypeName + " value:" + entry.getValue());
                for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
                    result.add(factoryTypeName, factoryImplementationName.trim());
                }
            }
        }
    }

}
